package com.bysj.lms.utils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class MailUtil {


    static JavaMailSender javaMailSender;

    @Autowired
    public void setJavaMailSender(JavaMailSender javaMailSenderAutowired){
        javaMailSender = javaMailSenderAutowired;
    }

    public static void sendEmail(String subject,String toMail,String text){
        // 构建一个邮件对象
        SimpleMailMessage message = new SimpleMailMessage();
        //邮件设置
        //设置邮件主题
        message.setSubject(subject);
        //设置邮件发送者，这个跟application.yml中的设置要一致
        message.setFrom("1473727528@qq.com");
        //设置邮件接收者，可以有多个接收者，中间用逗号隔开
        message.setTo(toMail);
        //设置邮件发送日期
        message.setSentDate(new Date());
        //设置邮件的正文
        message.setText(text);
        //发送邮件
        javaMailSender.send(message);
    }
}
